/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog'
], function ($, common, ready, Collection, template, Model, dialog) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'note_preset_list',
                model: {
                    url: _global.url.api + 'note_preset',
                    event: {
                        '.item-remove:click': 'remove',
                        '.item-edit:click': 'edit',
                    },
                    remove: function (e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function () {
                                if (model.del()) {
                                    _this.collection.reload();
                                }
                                ;
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    },
                    edit: function (e) {
                        var __this = {};
                        __this.data = this.data;
                        e.preventDefault();
                        _this.dialog = dialog({
                            title: '编辑',
                            content: template('edit-tpl', __this.data),
                            width: 700,
                            ok: function () {
                                var data = {};
                                data.id = __this.data.id;
                                data.act = 'edit';
                                data.pre_share_count = $("[name=pre_share_count]").val();
                                data.pre_like_count = $("[name=pre_like_count]").val();
                                data.is_enabled = $("[name=is_enabled]:checked").val();
                                if (new Model({
                                        url: _global.url.api + 'note_preset',
                                        data: data
                                    }).put()) {
                                    _this.collection.reload();
                                }
                            }, cancel: function () {

                            }
                        });
                        _this.dialog.show();
                    }
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function () {
            this.data = {};
            var _this = this;
            //搜索
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.reload();
            });
            //保存
            $('#btn-add').on('click', function (e) {
                _this.data = {};
                e.preventDefault();
                _this.dialog = dialog({
                    title: '添加',
                    content: template('add-tpl', {}),
                    width: 700,
                    ok: function () {
                        if ($('#note_id').val() == undefined) {
                            Alert({msg: '请填写商品id进行搜索', type: 'error'});
                            return false;
                        }
                        var data = {};
                        data.note_id = $("#note_id").val();
                        data.pre_share_count = $("[name=pre_share_count]").val();
                        data.pre_like_count = $("[name=pre_like_count]").val();
                        data.is_enabled = $("[name=is_enabled]:checked").val();

                        if (data.pre_share_count == '' || data.pre_user_rebate == '') {
                            Alert({msg: '请填写相关信息', type: 'error'});
                            return false;
                        }

                        if (new Model({
                                url: _global.url.api + 'note_preset',
                                data: data
                            }).post()) {
                            _this.collection.reload();
                        }
                    }, cancel: function () {
                    }
                });
                _this.dialog.show();
                $('#btn-note-search').on('click', function (e) {
                    note_id = $('#keyword').val();
                    if (note_id == '') {
                        Alert({msg: '请填写笔记ID进行搜索', type: 'error'});
                        return false;
                    }
                    $.ajax({
                        url: _global.url.api + 'note',
                        data: {note_id: note_id},
                        type: 'get',
                        async: false,
                        success: function (res) {
                            if (res.code == 0) {
                                $('#add_form').html(template('edit-tpl', res.data));
                                $('.long_date').on('click', function () {
                                    WdatePicker({
                                        el: this.id,
                                        dateFmt: 'yyyy-MM-dd HH:mm:ss'
                                    });
                                });
                            } else {
                                Alert({msg: res.msg ? res.msg : '未知错误', type: 'error'});
                            }
                        }
                    });
                });
                $('input,select,textarea').on('change', function (e) {
                    _this.change(e);
                });
            });

        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }
    };

    new index();
});